<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['ischecklogin']=cfc('core')->checklogin('jy_videomerch/merch/','',true);
$merchid=$_RQ['merchid']?:$_SESSION['merchid'];
if(!$this->setting['zc_merch']){
	if($merchid){
		$merch=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		if(empty($merch)){
			$merchid=0;
			$_SESSION['merchid']=0;
		}
	}
	$_SESSION['forward_url']='jy_videomerch/myhome/';
	$_SESSION['merch_url']='jy_videomerch/merch/';
	$merch=model('jy_merch/model')->checkmerch();
	$merchid=$merch['id'];
}else{
	$merchid=model('jy_merch/model')->checkadd_merch('','',$merchid);
}
$_SESSION['merchid']=$_SESSION['onemerchid']=$merchid;
if($_RQ['op']=='main'){
	$return['pluginset']['kf_logo']=tomedia($return['pluginset']['kf_logo']);
	$_SESSION['current_plugin']='jy_videomerch';
	$_SESSION['logout_forward']='jy_videomerch/merch/';
	$return['item']=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$return['mygroup']=model('jy_merch/model')->get_groupset($merchid);
	$return['tips']=model('jy_merch/model')->checkviptime($return['item']);	
	if($return['item']['viptime'] && $return['item']['viptime']>TIMESTAMP){
		$return['item']['viptime2']=date('Y-m-d',$return['item']['viptime']);
	}elseif($return['item']['viptime'] && $return['item']['viptime']<TIMESTAMP){
		$return['item']['viptime2']='已过期';
	}
	$return['isopenvoice']=$return['item']['isopenvoice'];
	$return['merchs']=pdo_getall('jy_merch_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['plugin_icon']=$return['item']['logo'];
	$this->session('plugin_icon',$return['item']['logo']);
	$avatar=pdo_getcolumn('core_members',array('id'=>$return['item']['mid'],'uniacid'=>$_SESSION['uniacid']),'avatar');
	$return['item']['logo']=$return['item']['logo']?$return['item']['logo']:$avatar;
	$return['item']['logo']=tomedia($return['item']['logo']);
	$return['item']['url']=custom_url('jy_videomerch/merch//id='.$merchid,true);
	$return['item']['payurl']=custom_url('jy_merch/member.recharge//merchid='.$merchid,true);
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'createtime >'=>$daytime);
	$return['looks']=pdo_count('jy_merch_game_looklog',$params);
	$params['ispay']=1;
	$return['ordernum']=pdo_count('jy_merch_game_order',$params);
	$return['orderfee']=pdo_getsum('jy_merch_game_order',$params,'fee');
	$return['params']=array('daytype'=>0,'time'=>'','qushi'=>0,'qushitype'=>0);
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>0);
	$params['status']=1;
	$return['status1']=pdo_count('jy_weishop_order',$params);
	$params['status']=0;
	$return['ispay']=pdo_count('jy_weishop_order',$params);
	$params['status']=2;
	$return['status2']=pdo_count('jy_weishop_order',$params);
	$return['goods']=pdo_count('jy_weishop_goods',array('type <>'=>4,'status <>'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
	$return['delgoods']=pdo_count('jy_weishop_goods',array('status'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
	$return['verifygoodsnum']=pdo_count('jy_weishop_verifygoods_log',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'salerid'=>$_SESSION['uid']),'id');
	$return['verifynum']=pdo_count('jy_weishop_order_verify',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clerk_id'=>$_SESSION['uid']),'id');
	$return['selfverifynum']=pdo_count('jy_weishop_order_self',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clerk_id'=>$_SESSION['uid']),'id');
	//活动数据
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	$params['status']=1;
	$return['gamestatus1']=pdo_count('jy_merch_game_order',$params);
	$params['status']=0;
	$return['gameispay']=pdo_count('jy_merch_game_order',$params);
	$params['status']=2;
	$return['gamestatus2']=pdo_count('jy_merch_game_order',$params);
	$return['allgames']=$this->allgames;
	foreach($return['allgames'] as &$av){
		$av['icon']=tomedia($av['icon']);
	}
	$storeurl='jy_videomerch/myshop//id='.$merchid;
	if($_SESSION['wxapp_id']){
		$image='oss/'.$_SESSION['uniacid'].'/images/jy_videomerch/myshop/wxappid'.$merchid.'.jpg';
		$isadd=cfc('files')->source_check($image);
		if(!$isadd){
			$url=wxapp_links($storeurl);
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
			if($_SESSION['fromh5wxapp']){
				$url=$storeurl;
				$url='/pages/index/index?url='.urlencode($url);
			}
			$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
			$path='oss/'.$_SESSION['uniacid'].'/images/jy_videomerch/myshop';
			if(!file_exists(ROOT_AT.$path)){
				mkdir(ROOT_AT.$path,0700, true);
			}
			if($res){
				cfc('files')->source_save($res,'nochoose','png','二维码',$image,'',$url);
			}
		}
		$return['dianewm']=tomedia($image);
	}
	$_SESSION['forward_url']=$_AR['access_json'];
	set_html('title','商户后台');
	exi($return);
}
if($_RQ['op']=='getdata'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	//创建时间
	if($_RQ['tstart'] && $_RQ['tend']){
		$params['createtime between']=array($_RQ['tstart'],$_RQ['tend']);
	}else{
		$daytype=intval($_RQ['daytype']);
		if($daytype==0){
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$params['createtime >']=$daytime;
		}elseif($daytype==1){
			$yesdaytime = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$params['createtime between']=array($yesdaytime,$daytime);
		}elseif($daytype==2){
			$day7time = mktime(0,0,0,date('m'),date('d')-7,date('Y'));
			$params['createtime >']=$day7time;
		}elseif($daytype==3){
			
		}
	}
	//print_r($params);exit;
	$params['ispay']=1;
	$data['paybi']=pdo_count('jy_weishop_order',$params);
	$data['payren']=pdo_count('jy_weishop_order',$params);
	$data['payyuan']=pdo_getsum('jy_weishop_order',$params,'price');
	$data['payjian']=pdo_getsum('jy_weishop_order_goods',$params,'total');
	$data['shoubi']=pdo_count('jy_weishop_order',$params);
	$data['shouren']=pdo_count('jy_weishop_order',$params);
	$data['shouyuan']=pdo_getsum('jy_weishop_order',$params,'price');
	$data['shoujian']=pdo_getsum('jy_weishop_order_goods',$params,'total');
	$return['params']=$params;
	if($_SESSION['os']=='windows'){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$access['status_day']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime));
		$access['status_all']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid']));
		$access['status1_day']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3),'createtime >'=>$daytime));
		$access['status1_all']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3)));
		//月份
		for($i=0;$i<12;$i++){
					if($i==0){
						$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
						$yueend = time();
						
					}else{
						$yuestart = mktime(0,0,0,date('m')-$i,1,date('Y'));
						$yueend = mktime(0,0,0,date('m')-$i+1,1,date('Y'));
					}
					$params=array('uniacid'=>$_SESSION['uniacid']);
					$params['createtime between']=array($yuestart,$yueend);
					$month12[$i] = date('Y-m',strtotime('-'.$i.' month'));
					//$monthaccess[]=pdo_count('core_access_log',$params);
					$accessline[$i]=pdo_count('jy_weishop_order',$params);
					//$monthcredit2[]=pdo_getsum('core_members',$params,'credit2');
		}
		$accessline=array_reverse($accessline);
		$month12=array_reverse($month12);
		$htmls=array(
					array('type'=>'data','tpltype'=>4,'one_num'=>4,'title'=>'今日/总订单数据指标','datas'=>array(
						array('title'=>'今日/总订单数','num'=>2,'data'=>array($access['status_day'],$access['status_all']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'今日/总成交数','bgcolor'=>'#009933','num'=>2,'data'=>array($access['status1_day'],$access['status1_all']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'待发货/收货数','bgcolor'=>'#3366FF','num'=>2,'data'=>array($return['status1'],$return['status2']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'今日/总订单数','bgcolor'=>'#FF3300','num'=>2,'data'=>array($access['pc_day'],$access['status_all']),'datatitle'=>array('客户数','申请数')),
					)),
					array('type'=>'echarts_arealine','ecid'=>'mobile','title'=>'月度订单数据','datas'=>array(
						'datas'=>array(
							array('name'=>'订单数','value'=>$accessline),
						),
						'xdatas'=>$month12,
					)),
		);	
		//$return['htmls']=$htmls;
	}
	$return['data']=$data;
	exi($return);
}